import sys,getopt
from data import Data,Router,Req

class LTE:
    def __init__(self,port):
        self.port = port
    def setd(self,fc,gain,sr):
        self.dev = Data()
        self.dev.fc = fc
        self.dev.gain = gain
        self.dev.sr = sr
        self.dev.module = "dev"
        self.dev.mode = "lte"
        self.dev.port = self.port
        self.view = Data()
        self.view.module = "ltev"
        self.view.port = self.port
        self.view.mode = "lte"
    
    def newdev(self):
        h = Router("new")
        req = h.handle("dev%d"%self.port)+'?'+self.dev.urldata()
        Req(req)

    def startdev(self):
        h = Router("start")
        req = h.handle("dev%d"%self.port)
        Req(req)

    def newview(self):
        h = Router("new")
        req = h.handle("ltev%d"%self.port)+'?'+self.view.urldata()
        Req(req)

    def resetppm(self):
        h = Router("resetppm")
        req = h.handle("ltev%d"%self.port)
        Req(req)
    
    def setfcmd(self,setf):
        if setf:
            return '?setf=true'
        else:
            return ''

    def ltepss(self,setf=True):
        h = Router("ltepss")
        req = h.handle("ltev%d"%self.port)+self.setfcmd(setf)
        Req(req)

    def ltescan(self,setf=True):
        h = Router("ltescan")
        req = h.handle("ltev%d"%self.port)+self.setfcmd(setf)
        Req(req)
    
    def ltepeak(self):
        h = Router("ltepeak")
        req = h.handle("ltev%d"%self.port)
        Req(req)
    
    def ltepeakWithID(self,cid):
        h = Router("ltepeak")
        req = h.handle("ltev%d"%self.port)+("?id=%d"%cid)
        Req(req)

    def ltecfo(self,setf=True):
        h = Router("ltecfo")
        req = h.handle("ltev%d"%self.port)+self.setfcmd(setf)
        Req(req)        
    
    def afcon(self):
        h = Router("afc")
        req = h.handle("ltev%d"%self.port)+'?on=true'
        Req(req)  

    def afcoff(self):
        h = Router("afc")
        req = h.handle("ltev%d"%self.port)+'?on=false'
        Req(req) 

    def newall(self):
        d = Data()
        d.module = "ltesv"
        d.port = self.port
        h = Router("new")
        req = h.handle("ltesv%d"%self.port)+'?'+d.urldata()
        Req(req)

    def lteall(self,txp,n):
        d = Data()
        d.txp = txp
        d.n = n
        h = Router("ltepeakall")
        req = h.handle("ltesv%d"%self.port)+'?'+d.urldata()
        Req(req) 

def ltestart(port):
    l = LTE(port)
    l.setd(874.2e6,40.,1.92e6)
    l.newdev()
    l.startdev()
    l.newview()
    l.afcoff()
    l.resetppm()
    l.ltepss()
    l.ltescan()
    l.ltepeak()
    l.ltecfo()
    l.afcon()

def ltestop(port):
    l = LTE(port)
    l.afcoff()

def ltecid(port,cid):
    l = LTE(port)
    l.ltepeakWithID(cid)  

def lteAll(port,txp,n):
    l = LTE(port)
    l.newall()
    l.lteall(txp,n)  

def main():
    opts, args = getopt.getopt(sys.argv[1:], "-a-s-p:-c:-t:", ["all","stop", "port=","cid=","txp="])
    port = 0
    stop = False
    cid = -1
    txp = 0
    all = False
    for opt_name,opt_value in opts:
        if opt_name in ('-p','--port'):
            port = int(opt_value)
        if opt_name in ('-s','--stop'):
            stop = True
        if opt_name in ('-c','--cid'):
            cid = int(opt_value)
        if opt_name in ('-t','--txp'):
            txp = int(opt_value)
        if opt_name in ('-a','--all'):
            all = True
    if all:
        if txp==-1:
            for txp in range(4):
                lteAll(port,txp,3)
        else:
            lteAll(port,txp,3)
        return

    if cid!=-1:
        ltecid(port,cid)
        return    
    
    if stop:
        ltestop(port)
    else:
        ltestart(port)

if __name__ == "__main__":
    main()